<include file="public@header"/>
<style type="text/css">
    .t-readonly {width:28%;float:left}
    .s-readonly {float:left;line-height:36px;}
</style>
</head>
<body>
<div class="wrap js-check-wrap">
    <ul class="nav nav-tabs">
        <li><a href="{:url('AdminLogi/index')}">订单管理</a></li>
        <li><a href="{:url('AdminLogi/add')}">添加订单</a></li>
        <li class="active"><a href="#">编辑订单</a></li>
    </ul>
    <form action="{:url('AdminLogi/editPost')}" method="post" class="form-horizontal js-ajax-form margin-top-20">
        <div class="row">
            <div class="col-md-9">
                <table class="table table-bordered">
                    <tr>
                        <th>专属编号<span class="form-required">*</span></th>
                        <td>
                            <input class="form-control code" type="text" name="post[code]" id="code" required value="{$post.code}" placeholder="请输入专属编号"/>
                        </td>
                    </tr>
                    <tr>
                        <th width="100">分类</th>
                        <td>
                            <input class="form-control" type="text" style="width:400px;"
                                   value="{:implode(' ',$post_categories)}"
                                   placeholder="请选择分类" onclick="doSelectCategory();" id="js-categories-name-input"
                                   readonly/>
                            <input class="form-control" type="hidden" value="{$post_category_ids}"
                                   name="post[categories]"
                                   id="js-categories-id-input"/>
                        </td>
                    </tr>
                    <tr>
                        <th>订单标题<span class="form-required">*</span></th>
                        <td>
                            <input class="form-control" type="text" name="post[post_title]" id="post_title" required value="{$post.post_title}" placeholder="请输入订单标题"/>
                        </td>
                    </tr>
                    <tr>
                        <th>收货人</th>
                        <td>
                            <input class="form-control" type="text" name="post[receive]" id="receive" value="{$post.receive}" placeholder="请输入收货人"/>
                        </td>
                    </tr>
                    <tr>
                        <th>联系方式</th>
                        <td>
                            <input class="form-control" type="text" name="post[contact]" id="contact" value="{$post.contact}" placeholder="请输入联系方式"/>
                        </td>
                    </tr>
                    <tr>
                        <th>下单用户</th>
                        <td>
                            <input readonly class="form-control user_id" type="text" name="user_name" value="" placeholder="请输入下单用户"/>
                            <input type="hidden" name="post[user_id]" id="user_id" value="">
                        </td>
                    </tr>
                    <tr>
                        <th>驿站物流地址</th>
                        <td>
                            <input readonly class="form-control stage" type="text" name="post[more][stage]" value="" placeholder="请输入驿站物流地址"/>
                            <input type="hidden" name="post[stage]" id="stage" value="">
                        </td>
                    </tr>
                    <tr>
                        <th>小站配送地址<span class="form-required">*</span></th>
                        <td>
                            <input readonly class="form-control station" type="text" name="post[more][station]" value="" placeholder="请输入小站配送地址"/>
                            <input type="hidden" name="post[station]" id="station" value="">
                        </td>
                    </tr>
                    <tr>
                        <th>用户收货地址<span class="form-required">*</span></th>
                        <td>
                            <input readonly class="form-control addr" type="text" name="post[more][user_addr]" value="" placeholder="请输入用户收货地址"/>
                            <input type="hidden" name="post[addr]" id="addr" value="">
                        </td>
                    </tr>
                    <tr>
                        <th>称重（kg）<span class="form-required">*</span></th>
                        <td>
                            <input class="form-control" type="number" name="post[weight]" id="weight" required value="{$post.weight}" placeholder="请输入称重（kg）"/>
                        </td>
                    </tr>
                    <tr>
                        <th>价格策略<span class="form-required">*</span></th>
                        <td>
                            <select class="form-control style" name="post[price_id]">
                                <notempty name="price">
                                <foreach name="price" item="v">
                                <option value="{$v.id}">{$v.name}</option>
                                </foreach>
                                </notempty>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>总费用</th>
                        <td>
                            <input readonly class="form-control total t-readonly" name="post[more][total]" type="text" placeholder="0"/>
                            <span class="s-readonly">&nbsp;=&nbsp;</span>
                            <input readonly class="form-control piece t-readonly" name="post[more][piece]" type="text" placeholder="0">
                            <span class="s-readonly">&nbsp;+&nbsp;</span>
                            <input readonly class="form-control price t-readonly" name="post[more][price]" type="text" placeholder="0">
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col-md-3">
                <table class="table table-bordered">
                    <tr>
                        <th>配送程序</th>
                    </tr>
                    <tr>
                        <td>
                        <if condition="$post['user_mail_type'] eq 1">送货上门
                        <elseif condition="$post['user_mail_type'] eq 2">自提
                        <else/>待用户确认</if>
                        </td>
                    </tr>
                    <!-- <tr>
                        <td>
                            <div class="checkbox">
                                <label><input type="checkbox" id="pay_status-checkbox" name="post[mail_type]" value="1" <if condition="$post['mail_type'] eq 1">checked</if>>支持送货上门</label>
                            </div>
                        </td>
                    </tr> -->
                    <tr>
                        <th><b>下单时间</b></th>
                    </tr>
                    <tr>
                        <td>
                            <input class="form-control js-bootstrap-datetime" type="text" name="post[published_time]" value="{$post.published_time|date='Y-m-d H:i:s',###}">
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <input type="hidden" name="post[id]" value="{$post.id}">
                <button type="submit" class="btn btn-primary js-ajax-submit">{:lang('SAVE')}</button>
                <a class="btn btn-default" href="javascript:history.back(-1);">{:lang('BACK')}</a>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="__STATIC__/js/admin.js"></script>

<script type="text/javascript">
    $(function(){
        $(".code").change();
        $("#weight").change();
    });
    function doSelectCategory() {
        var selectedCategoriesId = $('#js-categories-id-input').val();
        openIframeLayer("{:url('AdminCategory/select')}?ids=" + selectedCategoriesId, '请选择分类', {
            area: ['700px', '400px'],
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                var iframeWin          = window[layero.find('iframe')[0]['name']];
                var selectedCategories = iframeWin.confirm();
                if (selectedCategories.selectedCategoriesId.length == 0) {
                    layer.msg('请选择分类');
                    return;
                }
                $('#js-categories-id-input').val(selectedCategories.selectedCategoriesId.join(','));
                $('#js-categories-name-input').val(selectedCategories.selectedCategoriesName.join(' '));
                layer.close(index); 
            }
        });
    }
    var toCodeUrl = "{:cmf_url('api/getCode')}";
    var toPriceUrl = "{:cmf_url('api/getPrice')}";
    $(".code").change(function() {
        var code = $('.code').val();
        if (code.length>8) {
            $.ajax({ url: toCodeUrl,type:"post", dataType:"json",data: {code:code}, success: function(data){
                if(data.status !=1){
                    nody_msg('编号有误');
                }else{
                    if (data.result.user_addr_arr==null || data.result.stage_arr==null || data.result.station_arr==null ) {
                        nody_msg('编号信息有误');
                        $('.code').focus();
                        $('.form-ok').val(0);
                    } else {
                        $('.form-ok').val(1);
                        $('.user_id').val(data.result.user_name);
                        $('#user_id').val(data.result.user_id);
                        $('.addr').val(data.result.user_addr_arr.name);
                        $('#addr').val(data.result.user_addr_arr.id);
                        $('.stage').val(data.result.stage_arr.name);
                        $('#stage').val(data.result.stage_arr.id);
                        $('.station').val(data.result.station_arr.name);
                        $('#station').val(data.result.station_arr.id);

                        $('#post_title').val(data.result.post_title);
                        $('#contact').val(data.result.contact);
                        $('#receive').val(data.result.receive);
                    }
                }
            }});
        }
    });

    $("#weight").change(function() {
        var weight = $('#weight').val();
        var style = $('.style').val();
        changePrice(weight,style);
    });
    $(".style").change(function() {
        var weight = $('#weight').val();
        var style = $('.style').val();
        changePrice(weight,style);
    });
    // $("#pay_status-checkbox").change(function() {
    //     var weight = $('#weight').val();
    //     var style = $('.style').val();
    //     changePrice(weight,style);
    // });

    function changePrice(weight,style) {
        // var checked = $('#pay_status-checkbox').is(':checked');
        var checked = true;
        $.ajax({ url: toPriceUrl,type:"post", dataType:"json",data: {weight:weight,style:style}, success: function(data){
            if(data.status ==1){
                if ( checked==true ) {
                    $('.total').val(data.result.total);
                    $('.piece').val(data.result.piece);
                    $('.price').val(data.result.price);
                }else{
                    $('.total').val(data.result.piece);
                    $('.piece').val(data.result.piece);
                    $('.price').val(0);
                }
            } else {
                nody_msg('输入有误');
            }
        }});
    }

    function nody_msg(msg) {
        noty({
            text: msg,
            type: 'error',
            layout: 'center',
            animation:{  open:'animated fadeIn', speed:100 },
            modal: true,
            timeout: 400,
            callback: {}
        }).show();
    }

</script>


</body>
</html>
